package com.foldingleaf.withdraw.service.impl;

import java.util.List;
import com.foldingleaf.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.foldingleaf.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.foldingleaf.withdraw.domain.UserBankCard;
import com.foldingleaf.withdraw.mapper.WithdrawRecordMapper;
import com.foldingleaf.withdraw.domain.WithdrawRecord;
import com.foldingleaf.withdraw.service.IWithdrawRecordService;

/**
 * 提现记录Service业务层处理
 * 
 * @author ajian
 * @date 2022-06-22
 */
@Service
public class WithdrawRecordServiceImpl implements IWithdrawRecordService 
{
    @Autowired
    private WithdrawRecordMapper withdrawRecordMapper;

    /**
     * 查询提现记录
     * 
     * @param recordId 提现记录主键
     * @return 提现记录
     */
    @Override
    public WithdrawRecord selectWithdrawRecordByRecordId(Long recordId)
    {
        return withdrawRecordMapper.selectWithdrawRecordByRecordId(recordId);
    }

    /**
     * 查询提现记录列表
     * 
     * @param withdrawRecord 提现记录
     * @return 提现记录
     */
    @Override
    public List<WithdrawRecord> selectWithdrawRecordList(WithdrawRecord withdrawRecord)
    {
        return withdrawRecordMapper.selectWithdrawRecordList(withdrawRecord);
    }

    /**
     * 新增提现记录
     * 
     * @param withdrawRecord 提现记录
     * @return 结果
     */
    @Transactional
    @Override
    public int insertWithdrawRecord(WithdrawRecord withdrawRecord)
    {
        withdrawRecord.setCreateTime(DateUtils.getNowDate());
        int rows = withdrawRecordMapper.insertWithdrawRecord(withdrawRecord);
        insertUserBankCard(withdrawRecord);
        return rows;
    }

    /**
     * 修改提现记录
     * 
     * @param withdrawRecord 提现记录
     * @return 结果
     */
    @Transactional
    @Override
    public int updateWithdrawRecord(WithdrawRecord withdrawRecord)
    {
        withdrawRecordMapper.deleteUserBankCardByUserId(withdrawRecord.getRecordId());
        insertUserBankCard(withdrawRecord);
        return withdrawRecordMapper.updateWithdrawRecord(withdrawRecord);
    }

    /**
     * 批量删除提现记录
     * 
     * @param recordIds 需要删除的提现记录主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWithdrawRecordByRecordIds(Long[] recordIds)
    {
        withdrawRecordMapper.deleteUserBankCardByUserIds(recordIds);
        return withdrawRecordMapper.deleteWithdrawRecordByRecordIds(recordIds);
    }

    /**
     * 删除提现记录信息
     * 
     * @param recordId 提现记录主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWithdrawRecordByRecordId(Long recordId)
    {
        withdrawRecordMapper.deleteUserBankCardByUserId(recordId);
        return withdrawRecordMapper.deleteWithdrawRecordByRecordId(recordId);
    }

    /**
     * 新增用户银行卡绑定信息
     * 
     * @param withdrawRecord 提现记录对象
     */
    public void insertUserBankCard(WithdrawRecord withdrawRecord)
    {
        List<UserBankCard> userBankCardList = withdrawRecord.getUserBankCardList();
        Long recordId = withdrawRecord.getRecordId();
        if (StringUtils.isNotNull(userBankCardList))
        {
            List<UserBankCard> list = new ArrayList<UserBankCard>();
            for (UserBankCard userBankCard : userBankCardList)
            {
                userBankCard.setUserId(recordId);
                list.add(userBankCard);
            }
            if (list.size() > 0)
            {
                withdrawRecordMapper.batchUserBankCard(list);
            }
        }
    }
}
